//
//  BDNavigationController.m
//  BeidouMall
//
//  Created by qie on 16/3/28.
//  Copyright © 2016年 qie. All rights reserved.
//

#import "BDNavigationController.h"
#import "BDOrderDetailViewController.h"
#import "BDOrderHomeViewController.h"

@interface BDNavigationController ()

@end

@implementation BDNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bg3"] forBarMetrics:UIBarMetricsDefault];
    self.navigationBar.translucent = NO;
    self.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:RGBColor(0, 0, 0) forKey:NSForegroundColorAttributeName];
    [self hideNavgationBarBlackLine];
    [self dropShadowWithOffset:CGSizeMake(0, 0)
                        radius:2
                         color:[UIColor lightGrayColor]
                       opacity:0.8];
    
    //    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"ShiShangZhongHei" size:16.0],NSFontAttributeName,nil]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
    if (self.viewControllers.count > 0) {
        UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back4"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backBtnClick)];
        viewController.navigationItem.leftBarButtonItem = barButtonItem;
        viewController.view.clipsToBounds = YES;
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:YES];
    
}

- (void)backBtnClick {
    if ([self.topViewController isKindOfClass:[BDOrderDetailViewController class]]) {
        BDOrderDetailViewController *orderDetail = (BDOrderDetailViewController *)self.topViewController;
        if (orderDetail.notRefreshHome) {
            for (UIViewController *vc in self.viewControllers) {
                if ([vc isKindOfClass:[BDOrderHomeViewController class]]) {
                    BDOrderHomeViewController *orderHome = (BDOrderHomeViewController *)vc;
                    orderHome.notRefreshHome = YES;
                }
            }
        }
    }
    [self popViewControllerAnimated:YES];
}

- (void)hideNavgationBarBlackLine {
    if ([self.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]){
        NSArray *list=self.navigationBar.subviews;
        for (id obj in list) {
            if ([obj isKindOfClass:[UIImageView class]]) {
                UIImageView *imageView=(UIImageView *)obj;
                NSArray *list2 = imageView.subviews;
                for (id obj2 in list2) {
                    if ([obj2 isKindOfClass:[UIImageView class]]) {
                        UIImageView *imageView2=(UIImageView *)obj2;
                        imageView2.hidden = YES;
                    }
                }
            }
        }
    }
}

- (void)dropShadowWithOffset:(CGSize)offset
                      radius:(CGFloat)radius
                       color:(UIColor *)color
                     opacity:(CGFloat)opacity {
    
    // Creating shadow path for better performance
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, self.navigationBar.bounds);
    self.navigationBar.layer.shadowPath = path;
    CGPathCloseSubpath(path);
    CGPathRelease(path);
    
    self.navigationBar.layer.shadowColor = color.CGColor;
    self.navigationBar.layer.shadowOffset = offset;
    self.navigationBar.layer.shadowRadius = radius;
    self.navigationBar.layer.shadowOpacity = opacity;
    
    // Default clipsToBounds is YES, will clip off the shadow, so we disable it.
    self.navigationBar.clipsToBounds = NO;
    
}

@end
